package xyz.jangle.jdbc.test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

/**
 * JDBC批量更新
 * @author Administrator
 *
 */
public class JdbcBatchUpdate {

	public static void main(String[] args) {
		
		String url = "jdbc:mysql://127.0.0.1:3306/jangle_test?characterEncoding=UTF8&serverTimezone=Asia/Shanghai";
		String user = "root";
		String password = "111111";
		try(Connection con = DriverManager.getConnection(url, user, password);
			PreparedStatement ps = con.prepareStatement("update demo set age = ? where name = ?")){
			
			for (int i = 0; i < 3; i++) {
				ps.setInt(1, i*i);
				ps.setString(2, "test"+i);
				ps.addBatch();
			}
			int[] batch = ps.executeBatch();
			for (int i = 0; i < batch.length; i++) {
				System.out.println(batch[i]);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

	}

}
